﻿/*****************************************************************************
* File			: src.MiniBoss
* Purpose		: Contains Declaration and Implementation for miniboss class
* Creation date : April 25, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.display.MovieClip
	import flash.utils.Timer
	import flash.events.*
	import flash.filters.*
	import flash.display.*
	import flash.geom.Point
	
	/**************************************
	* Class		: MiniBoss
	* Purpose	: Logic for miniboss behavior
	* Parent	: MovieClip
	**************************************/
	public class MiniBoss extends MovieClip
	{
		// Class variables
		private 		var nXSpeed			:int;			// miniboss horizontal speed
		private 		var nYSpeed			:int;			// miniboss vertical speed

		private 		var nMargin			:int 	= 50;	// higher and lower margin for miniboss pop position
		
		private	 		var shootTimer		:Timer;			// Timer used to manage Missile shooting
		
		public 			var myFilters		:Array;			// holds an array of bitmap filters that can be applied to the ship
		
		public static 	var nPointValue		:int	=1000;	// Point givent when destroying this miniboss
		
		private			var nMaxHealth		:int	= 500;	// Maximum HP for this miniboss
		private			var nCurrentHealth	:int;			// HP for this miniboss
		
		
		// Constructor
		public function MiniBoss()
		{
			// Set listener for ENTER_FRAME event
			addEventListener(Event.ENTER_FRAME, enterFrame);
			cacheAsBitmap = true;
			
			// Stop miniboss timer
			LevelManager.main.miniBossTimer.stop();
			
			// Set the _x variable to 100px after the end of the background
			x = Game.WIDTH + 100;
			
			// Set the _y variable to a random position within [margin, background height - margin*2]
			y = nMargin + Math.random() * (Game.HEIGHT - nMargin*2);
			
			// Horizontal and vertical speed
			nXSpeed = 2;
			nYSpeed = 2;
			
			// Init health
			nCurrentHealth = nMaxHealth;
			
			// Add miniboss sprite
			Game.main.spriteClip.addChild(this);
			
			// Add miniboss to Enemy list
			Enemy.enemiesList.push(this);
			
			// Show HP meter
			Game.main.enemyHPMeter.visible		 = true;
			Game.main.enemyHPMeter.enemyBar.scaleX  	 = 1;
			
			//let's add a dropshadow filter to the ship for fun
			var filter:BitmapFilter = new DropShadowFilter(3,45,0x000000,50,5,5);
           	filters = [filter];
			
			//start firing missiles
			initShootingInterval();
		}
		
		/***********************************************************
		* Method		: enterFrame
		* Purpose		: frame loop
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public function enterFrame(e:Event)
		{
			// if the ship is not deleted
			if(Game.main.ship != null)
			{
				
				// miniboss movement
				// ------------------
				
				// Vertical movemetn management
				y += nYSpeed
				if(nYSpeed > 0 && y > 250)
					nYSpeed *= -1;
				else if(nYSpeed < 0 && y < 50)
					nYSpeed *= -1;
				
				// Horizontal movement management
				x -= nXSpeed;
				if(x < 400)
					nXSpeed = 0;
				
				// Manage collision with ship
				if(this.hitTestObject(Game.main.ship))
				{
					// Take damage
					takeDamage();
					
					// remove HP from ship
					Game.main.ship.updateHealth(-50);
				}
				// Collision management with Modules
				if(Game.main.ship.module1 != null && this.hitTestObject(Game.main.ship.module1))
				{				
					// Delete enemy instance
					takeDamage();
					
					// Remove health from ship
					Game.main.ship.module1.updateHealth(-50);
					return;
				}
				if(Game.main.ship.module2 != null && this.hitTestObject(Game.main.ship.module2))
				{				
					// Delete enemy instance
					takeDamage();
					
					// Remove health from ship
					Game.main.ship.module2.updateHealth(-50);
					return;
				}
			}
		}
		
		/***********************************************************
		* Method		: takeDamage
		* Purpose		: Remove health from the miniboss
		* Parameters	: None
		* return		: void
		***********************************************************/
		function takeDamage()
		{
			nCurrentHealth -= 10;
			
			// Update Health meter
			Game.main.enemyHPMeter.enemyBar.scaleX = nCurrentHealth/nMaxHealth;
			
			if(nCurrentHealth < 1)
			{
				// Create and display reward point
				var d = new RewardPoint(x, y - 5, String(MiniBoss.nPointValue), 0xCC6600);
				Game.main.spriteClip.addChild(d);
				
				// Launch miniboss timer again
				LevelManager.main.miniBossTimer.start();
				
				// update score
				Game.main.updateScore(MiniBoss.nPointValue);
				
				onDelete();
			}
		}
		
		/***********************************************************
		* Method		: initShootingInterval
		* Purpose		: Initialize shooting timer
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function initShootingInterval()
		{
			var interval:int = 2;	// Shoot every two seconds
			
			//create a timer instance to control it
			shootTimer = new Timer(interval * 1000, 0);
			shootTimer.addEventListener("timer", shoot);
			shootTimer.start();	
		}
				
		/***********************************************************
		* Method		: shoot
		* Purpose		: Manage the missile launch and position
		* Parameters	: e:TimerEvent
		* return		: void
		***********************************************************/
		function shoot(e:TimerEvent):void
		{
			// Create missiles instances
			new EnemyMissile(x - 10, y-20	, 5 , 6 + ( Math.random() * 2 ) ,0);
			new EnemyMissile(x - 10, y		, -5, 6 + ( Math.random() * 2 ) ,0);
			new EnemyMissile(x - 10, y+20	, 5 , 6 + ( Math.random() * 2 ) ,0);
		}

		/***********************************************************
		* Method		: onDelete
		* Purpose		: called for deleting the object instance
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function onDelete()
		{
			// stop shooting timer
			shootTimer.stop();
			shootTimer.removeEventListener("timer", shoot);
			
			// Add some explosions
			new Explosion(x-30, y - 5);
			new Explosion(x+30, y + 5);
			
			// Remove listener
			removeEventListener(Event.ENTER_FRAME, enterFrame);		
			
			// Remove enemy instance from list
			for(var i:int = 0; i < Enemy.enemiesList.length; i++)
			{
				if(Enemy.enemiesList[i] == this)
				{
					Enemy.enemiesList.splice(i,1);
					break;
				}
			}
			// Hide miniboss hp meter
			Game.main.enemyHPMeter.visible = false;
			
			// Set ship static miniboss variable to null
			Ship.miniBoss = null;			
			Game.main.spriteClip.removeChild(this);			
		}		
	}
}